package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.security.SecurityConst;
import com.base.security.entity.Resource;
import com.base.system.managedBean.baseBean.BaseResourceBean;

@ManagedBean(name = "modules.system.webadmin.editResourceBean", scope = ManagedBeanScope.REQUEST)
public class EditResourceBean extends BaseResourceBean {
	Log logger = LogFactory.getLog(EditSecurityRoleBean.class);
	@Inject
	FacesContext context;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			resource = (Resource) session
					.get(SecurityConst.RESOURCE_SELECT_ROW_DATA);
			if (resource == null) {
				String resourceId = context.getExternalContext()
						.getRequestParameterMap().get("resourceId");
				if (resourceId != null) {
					resource = securityEntityManager
							.getResourceById(resourceId);
				}
			}
			this.getResourceProperty();
		}
	}

	@Action(id = "saveResource")
	public void saveResource() {
		resource = securityEntityManager.getResourceById(id.toString());
		this.setResourceProperty();
		securityEntityManager.saveResource(resource);
		String script = "closeWindow()";
		Browser.execClientScript("window.parent", script);
		Browser.execClientScript("window.parent", "gridReload()");
	}
}
